<?php

namespace plugin\voice\app\admin\controller;

use plugin\admin\app\controller\Base;
use support\Request;
use support\Response;
use Webman\Medoo\Medoo as db;
/**
 * 配置管理
 */
class SettingController extends Base
{

    public function index(Request $request){
        $mini         = '';
        $mini_setting = '';
        $voice_mini   = config('plugin.voice_mini.app');
        if($voice_mini){
            $mini         = '<li>小程序设置</li>';
            $mini_setting = '<div class="layui-tab-item">
                              <form class="layui-form" lay-filter="setting" onsubmit="return false">
                                <div class="layui-form-item">
                                  <label class="layui-form-label">微信小程序AppID</label>
                                  <div class="layui-input-inline">
                                    <input type="text" name="mini_appid" required  lay-verify="required" autocomplete="off" class="layui-input">
                                  </div>
                                </div>
                                <div class="layui-form-item">
                                  <label class="layui-form-label">微信小程序Appsec</label>
                                  <div class="layui-input-inline">
                                    <input type="text" name="mini_appsec" required  lay-verify="required" autocomplete="off" class="layui-input">
                                  </div>
                                </div>
                                <div class="layui-form-item">
                                  <div class="layui-input-block">
                                    <button class="pear-btn pear-btn-primary" lay-submit="" lay-filter="setting">保 存</button>
                                  </div>
                                </div>
                              </form>
                            </div>';
        }
        return raw_view('setting/index',['voice_mini' => $mini,'mini_setting' => $mini_setting]);
    }

    public function select(){
        $setting = db::get('voice_settings','*');
        if($setting['yingying_enable'] == 1){
            $setting['yingying_enable'] = true;
        }else{
            $setting['yingying_enable'] = false;
        }
        if($setting['redis_enable'] == 1){
            $setting['redis_enable'] = true;
        }else{
            $setting['redis_enable'] = false;
        }
        return json([
            'code' => 0, 
            'msg'  => 'ok', 
            'data' => $setting
        ]);
    }

    public function update(Request $request){
        $setting = db::get('voice_settings','*');
        db::update('voice_settings',$request->post(),['id' => $setting['id']]);
        return $this->json(0);
    }

}
